
using UnityEngine;
using Unity.Mathematics;
public class Cesium3DTilesetRoot : MonoBehaviour
{
    public double3 _worldOriginLocation { get; set; }
    public double3 _absoluteLocation { get; set; }
    public double4x4 _tilesetToUnrealRelativeWorld { get; set; }

    private void Start()
    {
        this._updateAbsoluteLocation();
        this._updateTilesetToUnityRelativeWorldTransform();
    }

    void _updateAbsoluteLocation()
    {
        this._absoluteLocation = new double3(transform.position.x, transform.position.y, transform.position.z);
        this._worldOriginLocation = double3.zero;
    }

    public void HandleGeoreferenceUpdated()
    {
        this._updateTilesetToUnityRelativeWorldTransform();
    }


    void _updateTilesetToUnityRelativeWorldTransform()
     {
        Cesium3DTileset pTileset = GetComponent<Cesium3DTileset>();
        double4x4 ellipsoidCenteredToUnrealWorld = pTileset.ResolveGeoreference().GetGeoTransforms().GetEllipsoidCenteredToAbsoluteUnrealWorldTransform();

        double3 relativeLocation = this._absoluteLocation - this._worldOriginLocation;

        Matrix4x4 tilesetActorToUeLocal = transform.worldToLocalMatrix;
        double4x4 ueAbsoluteToUeLocal = VecMath.createMatrix4D(tilesetActorToUeLocal, relativeLocation);
        this._tilesetToUnrealRelativeWorld =math.mul(ueAbsoluteToUeLocal , ellipsoidCenteredToUnrealWorld) ;
        pTileset.UpdateTransformFromCesium();
    }
}